package com.yb.livewykt.util

import android.content.ContentResolver
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.GradientDrawable
import android.net.Uri
import android.os.Build
import android.provider.OpenableColumns
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.net.toFile
import com.yb.livewykt.LiveApplication
import com.yb.livewykt.bean.ErrorMsg
import java.io.File
import java.io.FileOutputStream
import java.util.*


fun String.showToast(duration:Int = Toast.LENGTH_SHORT){
    Toast.makeText(LiveApplication.context,this,duration).show()
}
fun netError() : ErrorMsg {
    return ErrorMsg(responseErrorCode, reponseErrorMsg)
}
fun dp2px(context: Context, dipValue: Float): Int {
    val scale = context.resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}

private val displayMetrics = Resources.getSystem().displayMetrics
fun getScreenWidth():Int{
    return displayMetrics.widthPixels
}
fun getScreenHeight():Int{
    return displayMetrics.heightPixels
}

@RequiresApi(Build.VERSION_CODES.Q)
fun uriToFileQ(context: Context, uri: Uri): File? =
    when (uri.scheme) {
        ContentResolver.SCHEME_FILE -> uri.toFile()
        ContentResolver.SCHEME_CONTENT -> {
            //把文件保存到沙盒
            val contentResolver = context.contentResolver
            val cursor = contentResolver.query(uri, null, null, null, null)
            cursor?.let {
                if (it.moveToFirst()) {
                    val ois = context.contentResolver.openInputStream(uri)
                    val displayName =
                        it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
                    ois?.let {
                        File(
                            context.externalCacheDir!!.absolutePath,
                            "${Random().nextInt(9999)}$displayName"
                        ).apply {
                            val fos = FileOutputStream(this)
                            android.os.FileUtils.copy(ois, fos)
                            fos.close()
                            it.close()
                        }
                    }
                } else null

            }

        }
        else -> null
    }

fun setStrokeShapeByRoundIng(color:Int) : GradientDrawable{
    val drawable = GradientDrawable()
    drawable.setStroke(6,color)
    return drawable
}








const val PHONE_NULL = "请输入手机号"
const val PASSWORD_NULL = "请输入密码"
const val responseSuccessCode:Int = 200
const val responseErrorCode:Int = 1
const val responseNoDataCode = 100010
const val reponseDefaultMsg:String = "操作成功"
const val reponseErrorMsg:String = "网络错误，请稍后重试"
const val responseTokenFailedCode:Int = 400000;
const val responseTokenFailedMsg:String = "登录失效，请重新登录";
const val uuid = "11111111111222"
const val IM_APP_KEY = "5c35b9162148f25b125e4af2d052134d"
const val EXIT_LIVE = 100
const val RECEIVE_GIFT = 300
const val NOT_INPUT_SOMETHING = "不可以发送空消息哦"
const val CHAT_ROOM_INPUT_CONNECT = "chat_room_connect"
